package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.BasePedigreeDao;
import com.linktel.productionLine.dao.ProductYieldOffsetDao;
import com.linktel.productionLine.entity.BasePedigree;
import com.linktel.productionLine.entity.ProductYieldOffset;
import com.linktel.productionLine.entity.dto.productYieldOffset.ProductYieldOffsetDTO;
import com.linktel.productionLine.entity.query.productionYieldOffset.AddProductYieldOffsetQuery;
import com.linktel.productionLine.entity.query.productionYieldOffset.EditProductYieldOffsetQuery;
import com.linktel.productionLine.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author HuangJiaHui
 * @Date 2023/9/19 10:09
 */
@Service
@AllArgsConstructor
public class ProductYieldOffsetService {
    private ProductYieldOffsetDao productionYieldOffsetDao;
    private BasePedigreeDao basePedigreeDao;

    /**
     * 查询所有产品良率补偿
     *
     * @return 良率补偿列表
     */
    public List<ProductYieldOffsetDTO> listProductYieldOffset() {
        return productionYieldOffsetDao.listProductYieldOffset();
    }

    /**
     * 新增产品良率补偿
     *
     * @param addProductYieldOffsetQuery 新增的产品良率补偿封装对象
     * @return 是否新增成功
     */
    public Boolean addProductYieldOffset(AddProductYieldOffsetQuery addProductYieldOffsetQuery) {
        String productSeriesCode = addProductYieldOffsetQuery.getProductSeriesCode();
        String productCode = addProductYieldOffsetQuery.getProductCode();

        ProductYieldOffset productionYieldOffset = new ProductYieldOffset();
        BeanUtils.copyProperties(addProductYieldOffsetQuery, productionYieldOffset);
        if ((productSeriesCode == null || "".equals(productSeriesCode))) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产品系列编码是必填项");
        }
        List<Long> productYieldOffset= productionYieldOffsetDao.getByProductCode(productSeriesCode, productCode);
        if (!CollectionUtils.isEmpty(productYieldOffset)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "已经存在该产品系列编码、产品编码对应的产品良率补偿");
        }

        BasePedigree basePedigree1 = basePedigreeDao.getByCode(productSeriesCode);
        productionYieldOffset.setProductSeriesId(basePedigree1.getId());
        productionYieldOffset.setProductSeriesName(basePedigree1.getName());

        if (productCode != null && !"".equals(productCode)) {
            BasePedigree basePedigree2 = basePedigreeDao.getByCode(productCode);
            productionYieldOffset.setProductId(basePedigree2.getId());
            productionYieldOffset.setProductName(basePedigree2.getName());
        }
        return productionYieldOffsetDao.addProductYieldOffset(productionYieldOffset, addProductYieldOffsetQuery.getCurrentUserId());
    }

    /**
     * 修改产品良率补偿
     *
     * @param editProductYieldOffsetQuery 要修改的产品良率补偿
     * @return 是否修改成功
     */
    @Transactional
    public Boolean editProductYieldOffset(EditProductYieldOffsetQuery editProductYieldOffsetQuery) {
        Long id = editProductYieldOffsetQuery.getId();
        String productSeriesCode = editProductYieldOffsetQuery.getProductSeriesCode();
        String productCode = editProductYieldOffsetQuery.getProductCode();
        ProductYieldOffset productionYieldOffset = new ProductYieldOffset();
        BeanUtils.copyProperties(editProductYieldOffsetQuery, productionYieldOffset);
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不能为空");
        }
        if ((productSeriesCode == null || "".equals(productSeriesCode))) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产品系列编码必填项");
        }
        BasePedigree basePedigree1 = basePedigreeDao.getByCode(productSeriesCode);
        productionYieldOffset.setProductSeriesId(basePedigree1.getId());
        productionYieldOffset.setProductSeriesName(basePedigree1.getName());

        if (productCode != null && !"".equals(productCode)) {
            BasePedigree basePedigree2 = basePedigreeDao.getByCode(productCode);
            productionYieldOffset.setProductId(basePedigree2.getId());
            productionYieldOffset.setProductName(basePedigree2.getName());
        }
        productionYieldOffsetDao.editProductYieldOffset(productionYieldOffset, editProductYieldOffsetQuery.getCurrentUserId());

        List<Long> productYieldOffset= productionYieldOffsetDao.getByProductCode(productSeriesCode, productCode);
        if (productYieldOffset.size() >= 2) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "已经存在该产品系列编码、产品编码对应的产品良率补偿");
        }
        return true;
    }

    /**
     * 删除产品良率补偿
     *
     * @param currentUserId 当前登录用户id
     * @param id            删除的产品良率补偿id
     * @return 是否删除成功
     */
    public Boolean deleteProductYieldOffset(Long currentUserId, Long id) {
        return productionYieldOffsetDao.deleteProductYieldOffset(currentUserId, id);
    }
}
